package com.rd.student.dashuju.sunwenzhuo.Test2;

public class TestBank extends Thread {

    Account account;

    double money;

    public TestBank(String name, Account account, double money) {
        super(name);
        this.account = account;
        this.money = money;
    }

    @Override
    public void run() {
        synchronized (account) {
            if (account.getBalance() < money) {
                System.out.println("余额不足");
                return;
            }
            System.out.println(getName() + "可以取钱");
            account.setBalance(account.getBalance() - money);
            System.out.println("取钱后的余额为：" + account.getBalance());
        }

    }

    public static void main(String[] args) {
        Account account = new Account("123456", 888);
        new TestBank("张三", account, 666).start();
        new TestBank("李四", account, 888).start();
    }
}

class Account {
    private String accountId;

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    private double balance;


    public Account(String accountId, double balance) {
        this.accountId = accountId;
        this.balance = balance;
    }
}
